package com.three.service.impl;

import com.baor.baseUtil.R;
import com.three.dao.DepartmentsDao;
import com.three.domain.Departments;
import com.three.domain.Students;
import com.three.service.DepartmentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class DepartmentsServiceImpl implements DepartmentsService {
    @Autowired
    private DepartmentsDao departmentsDao;
    @Override
    public R departmentsList(Integer pageIndex, Integer pageSize,String keyword) {
        pageIndex = (pageIndex - 1) * pageSize;
        double pageCountDepartments = departmentsDao.pageCountDepartments(keyword);
        System.out.println(pageCountDepartments);
        pageCountDepartments = Math.ceil(pageCountDepartments / pageSize);
        List<Departments> departmentsList = departmentsDao.departmentsList(pageIndex,pageSize,keyword);
        Map<String, Object> map = new HashMap<>();
        map.put("pageCountDepartments", pageCountDepartments);
        map.put("departmentsList", departmentsList);
        return R.ok(map);
    }

    @Override
    public R addDepartments(Departments departments) {
        return R.ok(departmentsDao.addDepartments(departments));
    }

    @Override
    public R updateDepartments(Departments departments) {
        return R.ok(departmentsDao.updateDepartments(departments));
    }

    @Override
    public R deleteDepartments(Integer department_id) {
        return R.ok(departmentsDao.deleteDepartments(department_id));
    }

    @Override
    public R List() {
        return R.ok(departmentsDao.List());
    }
}
